package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.WxUserDTO;
import com.ayf.payment.game.api.entity.Merchant;
import com.ayf.payment.game.api.entity.MerchantSon;
import com.ayf.payment.game.api.entity.platform.WxUser;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.platform.WxUserMapper;
import com.ayf.payment.game.api.service.MerchantService;
import com.ayf.payment.game.api.service.MerchantSonService;
import com.ayf.payment.game.api.service.platform.WxUserService;
import com.ayf.payment.game.api.type.LoginType;
import com.ayf.payment.game.api.wx.WxSendMsgUtils;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class WxUserServiceImpl implements WxUserService {

    @Resource
    private WxUserMapper wxUserMapper;
    @Autowired
    private WxSendMsgUtils wxSendMsgUtils;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private MerchantSonService merchantSonService;

    @Override
    public boolean addWxUser(WxUser wxUser) {
        return wxUserMapper.insertSelective(wxUser) > 0;
    }

    @Override
    public boolean changeWxUserSubscribe(WxUser wxUser) {
        WxUser tempWxUser = new WxUser();
        tempWxUser.setOpenid(wxUser.getOpenid());
        //根据openid查询出数据库中的用户信息
        WxUser dbWxUser = wxUserMapper.selectOne(tempWxUser);
        //判断用户是否存在，存在更改用户状态
        if (dbWxUser != null) {
//            dbWxUser.setMerchantId(null);
//            dbWxUser.setType(null);
//            dbWxUser.setLoginId(null);
            dbWxUser.setSubscribe(0);
            //根据主键更新关注状态
            return wxUserMapper.updateByPrimaryKey(dbWxUser) > 0;
        }
        return false;
    }

    @Override
    public boolean bindWx(WxUser wxUser, String loginAccount) {
        WxUser tempWxUser = new WxUser();
        tempWxUser.setOpenid(wxUser.getOpenid());
        WxUser dbWxUser = wxUserMapper.selectOne(tempWxUser);
        if (dbWxUser != null) {
            WxUser tempWxUser2 = new WxUser();
            tempWxUser2.setMerchantId(wxUser.getMerchantId());
            tempWxUser2.setLoginId(wxUser.getLoginId());
            WxUser dbWxUser2 = wxUserMapper.selectOne(tempWxUser2);

            if (dbWxUser2 != null) {
                if (dbWxUser2.getOpenid().equals(wxUser.getOpenid())) {
                    throw new ParameterException("绑定失败，账号" + loginAccount + "已绑定微信：" + dbWxUser2.getNickname() +
                            "，无需重复绑定。");
                }
                String why = "绑定失败，账号" + loginAccount + "已绑定微信：" + dbWxUser2.getNickname() + "，请先进行解绑";
                wxSendMsgUtils.bindWxFail(wxUser.getOpenid(), why, "PC端账号验证绑定");
                throw new ParameterException("绑定失败，该账号已绑定微信：" + dbWxUser2.getNickname());
            }
            if (dbWxUser.getLoginId() != null) {
                String why = "绑定失败，该微信已绑定账号" + dbWxUser.getLoginId() + "，请先进行解绑";
                wxSendMsgUtils.bindWxFail(wxUser.getOpenid(), why, "PC端账号验证绑定");
                throw new ParameterException("绑定失败，该微信已绑定账号" + dbWxUser.getLoginId() + "，请先进行解绑。");
            }
            Date date = new Date();
            wxSendMsgUtils.bindWx(wxUser.getOpenid(), loginAccount, date, "PC端账号验证绑定");

            wxUser.setBindTime(date);
            wxUser.setId(dbWxUser.getId());
            //根据主键更新关注状态
            return wxUserMapper.updateByPrimaryKeySelective(wxUser) > 0;
        }
        return false;
    }

    @Override
    public boolean unBindWx(Integer merchantId, Integer loginId) {
        WxUser wxUser = getBindWxUser(merchantId, loginId);
        if (wxUser != null) {
            String account = getAccount(wxUser.getLoginId(), wxUser.getType());
            wxSendMsgUtils.unbindWx(wxUser.getOpenid(),account);
            wxUser.setLoginId(null);
            wxUser.setMerchantId(null);
            wxUser.setType(null);
            return wxUserMapper.updateByPrimaryKey(wxUser) > 0;
        }
        return false;
    }

    @Override
    public WxUser getBindWxUser(Integer merchantId, Integer loginId) {
        WxUser wxUser = new WxUser();
        wxUser.setMerchantId(merchantId);
        wxUser.setLoginId(loginId);
        return wxUserMapper.selectOne(wxUser);
    }

    @Override
    public WxUser getWxUserByLoginAccount(String loginAccount) {
        return wxUserMapper.getWxUserByLoginAccount(loginAccount);
    }

    @Override
    public List<WxUser> getWxUser(Integer merchantId, Integer loginId) {
        WxUser wxUser = new WxUser();
        wxUser.setMerchantId(merchantId);
        wxUser.setLoginId(loginId);
        return wxUserMapper.select(wxUser);
    }

    @Override
    public List<WxUser> getWxUser(Integer merchantId) {
        WxUser wxUser = new WxUser();
        wxUser.setMerchantId(merchantId);
        return wxUserMapper.select(wxUser);
    }

    @Override
    public WxUser getWxUserByOpenid(String openid) {
        if (StringUtils.isEmpty(openid)) {
            throw new ParameterException("openid不能为空");
        }
        return wxUserMapper.selectWxUserByOpenid(openid);
    }

    @Override
    public String getAccountNameByOpenid(String openid) {
        WxUser wxUser = getWxUserByOpenid(openid);
        if (wxUser != null && wxUser.getType() != null) {
            if (wxUser.getType() == 1) {
                Merchant merchant = merchantService.find(wxUser.getLoginId());
                if (merchant != null) {
                    return merchant.getLoginAccount();
                }
            } else {
                MerchantSon merchantSon = merchantSonService.selectInfo(wxUser.getLoginId());
                if (merchantSon != null) {
                    return merchantSon.getLoginAccount();
                }
            }
        }
        return null;
    }

    @Override
    public List<WxUserDTO> getWxList(MerchantDTO dto) {
        WxUser wxUser = new WxUser();
        wxUser.setMerchantId(dto.getId());
        if (!LoginType.FATHER.name().equals(dto.getLoginType())) {
            wxUser.setLoginId(dto.getLoginId());
        }
        List<WxUser> select = wxUserMapper.select(wxUser);
        return BeanConvertUtils.convert(select, WxUserDTO.class);
    }

    @Override
    public boolean updateWxMsgStatus(WxUserDTO dto) {
        if (dto.getId() == null) {
            throw new ParameterException("微信表id不能为空");
        }
        WxUser wxUser = new WxUser();
        wxUser.setId(dto.getId());
        wxUser.setWxMsgStatus(dto.getWxMsgStatus());
        wxUser.setWxRechargeMsgStatus(dto.getWxRechargeMsgStatus());
        wxUser.setLoginRemindStatus(dto.getLoginRemindStatus());
        return wxUserMapper.updateByPrimaryKeySelective(wxUser) > 0;
    }

    @Override
    public String getAccount(Integer loginId, Integer type) {
        if (type == 1) {
            Merchant merchant = merchantService.find(loginId);
            return merchant.getLoginAccount();
        } else {
            MerchantSon merchantSon = merchantSonService.selectInfo(loginId);
            return merchantSon.getLoginAccount();
        }
    }
}